package com.situ.blog.dao.impl;

import com.situ.blog.dao.IBlogDao;
import com.situ.blog.pojo.Blog;
import com.situ.blog.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BlogDaoImpl implements IBlogDao {

    @Override
    public List<Blog> selectAll() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Blog> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT id,title,image,click_count,content,type_id,status,deleted,gmt_create,gmt_modified FROM blog where 1=1 ";
            preparedStatement = connection.prepareStatement(sql);
            System.out.println(preparedStatement);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {//判断下一个有没有，如果返回true而且指向下一个，没有返回false
                int id = resultSet.getInt("id");
                String title = resultSet.getString("title");
                String image = resultSet.getString("image");
                int clickCount = resultSet.getInt("click_count");
                String content = resultSet.getString("content");
                int typeId = resultSet.getInt("type_id");
                int status = resultSet.getInt("status");
                int deleted = resultSet.getInt("deleted");
                java.util.Date gmtCreate = resultSet.getDate("gmt_create");
                java.util.Date gmtModified = resultSet.getDate("gmt_modified");
                Blog blog = new Blog(id, title, image, clickCount, content, typeId, status, deleted, gmtCreate, gmtModified);
                list.add(blog);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, preparedStatement, resultSet);
        }

        return list;
    }

    @Override
    public Blog selectById(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Blog blog = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT id,title,image,click_count,content,type_id,status,deleted,gmt_create,gmt_modified FROM blog where id=? ";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            System.out.println(preparedStatement);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {//判断下一个有没有，如果返回true而且指向下一个，没有返回false
                String title = resultSet.getString("title");
                String image = resultSet.getString("image");
                int clickCount = resultSet.getInt("click_count");
                String content = resultSet.getString("content");
                int typeId = resultSet.getInt("type_id");
                int status = resultSet.getInt("status");
                int deleted = resultSet.getInt("deleted");
                java.util.Date gmtCreate = resultSet.getDate("gmt_create");
                java.util.Date gmtModified = resultSet.getDate("gmt_modified");
                blog = new Blog(id, title, image, clickCount, content, typeId, status, deleted, gmtCreate, gmtModified);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, preparedStatement, resultSet);
        }

        return blog;
    }
}
